﻿using dreamwork.mobile.app.api.Infrastructure;
using dreamwork.mobile.app.api.Models.Data;
using dreamwork.mobile.app.api.Models.ViewModel;
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading.Tasks;

namespace dreamwork.mobile.app.api.Services
{
    public class TerminalUserRegisterService : ITerminalUserRegisterService
    {
        private readonly ITerminalUserRepository _terminalUserRepository;
        public TerminalUserRegisterService(ITerminalUserRepository terminalUserRepository)
        {
            _terminalUserRepository = terminalUserRepository;
        }
        public TerminalUserRegisterService()
        {
        }
        public async Task<RegistrationCodeViewModel> GetRegistrationCodeAsync(string phoneNumber)
        {
            return await Task.FromResult(new RegistrationCodeViewModel { PhoneNumber=phoneNumber, RegistrationCode= "000001" });
        }

        public async Task<Tuple<bool, string>> RegisterAsync(TerminalUser user)
        {
            var model = await _terminalUserRepository.GetAsync(x => x.PhoneNumber == user.PhoneNumber);
            if (model != null)
            {
                return new Tuple<bool, string>(false, "该手机号已注册");
            }
            user.Password = GeneratePassWord(user.Password);
            await _terminalUserRepository.AddAsync(user);
            return new Tuple<bool, string>(true, "注册成功");
        }
        private string GeneratePassWord(string password)
        {
            return password;
        }
    }
}
